﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientLibrary;

namespace DocumentDigitalization
{
    public partial class Form_Templates : Form
    {
        SystemState Systemstate;

        public Form_Templates(SystemState ss)
        {
            this.Systemstate = ss;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Systemstate.CreateTemplateXML();
            this.Close();
        }

        private void btn_addnewfield_Click(object sender, EventArgs e)
        {
            this.listView_fields.Items.Add(this.tb_fieldname.Text);
            this.tb_fieldname.Clear();
            this.tb_fieldname.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Template template = new Template(this.tb_templatename.Text);
            for (int i = 0; i < this.listView_fields.Items.Count; i++)
            {
                Field field = new Field();
                field.Name = this.listView_fields.Items[i].Text;                
                template.getFields().Add(field);
            }
            this.Systemstate.getTemplates().Add(template);

            // creating the xml file
            this.Systemstate.CreateTemplateXML();

            this.listView_fields.Clear();
        }

        private void tabPage_viewtemplates_Enter(object sender, EventArgs e)
        {
            this.dataGridView_templates.Rows.Clear();
            for (int i = 0; i < this.Systemstate.getTemplates().Count; i++)
            {
                this.dataGridView_templates.Rows.Add(this.Systemstate.getTemplates()[i].Name);   
            }
        }

        private void dataGridView_templates_Click(object sender, EventArgs e)
        {
            /*
            try
            {
                //this.dataGridView_fields.Rows.Clear();
                string template_name = this.dataGridView_templates.SelectedRows[0].Cells[0].Value.ToString();
                for (int i = 0; i < this.Systemstate.getTemplates().Count; i++)
                {
                    if (this.Systemstate.getTemplates()[i].Name == template_name)
                    {
                        for (int j = 0; j < this.Systemstate.getTemplates()[i].getFields().Count; j++)
                        {
                            string[] cells = {this.Systemstate.getTemplates()[i].getFields()[j].Name, this.Systemstate.getTemplates()[i].getFields()[j].X.ToString(), this.Systemstate.getTemplates()[i].getFields()[j].Y.ToString(), this.Systemstate.getTemplates()[i].getFields()[j].Width.ToString(), this.Systemstate.getTemplates()[i].getFields()[j].Height.ToString()};
                            this.dataGridView_fields.Rows.Add(cells);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
             * */
        }

        private void dataGridView_templates_SelectionChanged(object sender, EventArgs e)
        {
            this.dataGridView_fields.Rows.Clear();
            try
            {
                
                //string[] c = { "0", "1", "2", "3", "4" };
                //this.dataGridView_fields.Rows.Add(c);
                string template_name = this.dataGridView_templates.SelectedRows[0].Cells[0].Value.ToString();
               
                for (int i = 0; i < this.Systemstate.getTemplates().Count; i++)
                {
                    if (this.Systemstate.getTemplates()[i].Name.Equals(template_name))
                    {                        
                        for (int j = 0; j < this.Systemstate.getTemplates()[i].getFields().Count; j++)
                        {
                            string[] cells = { this.Systemstate.getTemplates()[i].getFields()[j].Name, this.Systemstate.getTemplates()[i].getFields()[j].X.ToString(), this.Systemstate.getTemplates()[i].getFields()[j].Y.ToString(), this.Systemstate.getTemplates()[i].getFields()[j].Width.ToString(), this.Systemstate.getTemplates()[i].getFields()[j].Height.ToString() };
                            this.dataGridView_fields.Rows.Add(cells);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string template_name = this.dataGridView_templates.SelectedRows[0].Cells[0].Value.ToString();

                for (int i = 0; i < this.Systemstate.getTemplates().Count; i++)
                {
                    if (this.Systemstate.getTemplates()[i].Name.Equals(template_name))
                    {
                        this.Systemstate.getTemplates().RemoveAt(i);
                    }
                }

                this.tabPage_viewtemplates_Enter(null, null);
            }
            catch (Exception)
            {
                
            }
        }
    }
}
